package com.xyh.flowershop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyh.flowershop.domain.GoodTypeRel;
import com.xyh.flowershop.domain.Goods;
import com.xyh.flowershop.mapper.GoodTypeRelMapper;
import com.xyh.flowershop.mapper.GoodsMapper;
import com.xyh.flowershop.service.GoodsService;
import com.xyh.flowershop.utils.IdGenerator;
import com.xyh.flowershop.vo.req.goods.GoodPageReqVo;
import com.xyh.flowershop.vo.req.goods.GoodsEditReqVo;
import com.xyh.flowershop.vo.resp.goods.GoodsRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    private final GoodTypeRelMapper goodTypeRelMapper;

    public GoodsServiceImpl(GoodTypeRelMapper goodTypeRelMapper) {
        this.goodTypeRelMapper = goodTypeRelMapper;
    }

    @Override
    public Boolean addGoods(GoodsEditReqVo vo) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(vo, goods);
        vo.setTotalNum(Objects.isNull(vo.getTotalNum()) || vo.getTotalNum() == 0 ? 20 : vo.getTotalNum());
        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());
        int row = baseMapper.insert(goods);
        String goodsId = goods.getId();
        List<GoodTypeRel> rels = vo.getTypeIds().stream().map(item ->{
            GoodTypeRel rel = new GoodTypeRel();
            rel.setId(IdGenerator.nextId().toString());
            rel.setGoodId(goodsId);
            rel.setTypeId(item);
            return rel;
        }).toList();
        goodTypeRelMapper.batchInsert(rels);
        return row > 0;
    }

    @Override
    public Boolean editGoods(GoodsEditReqVo vo) {
        Goods goods = baseMapper.selectById(vo.getId());
        BeanUtils.copyProperties(vo,goods);
        goods.setUpdateTime(new Date());
        return baseMapper.updateById(goods) > 0;
    }

    @Override
    public Boolean deleteGoods(List<Long> ids) {
        int row = baseMapper.removeGoods(ids);
        goodTypeRelMapper.removeByGoodId(ids);
        return row > 0;
    }

    @Override
    public IPage<GoodsRespVo> findPageGoods(GoodPageReqVo vo) {
        if(vo.getPageIndex() == null || vo.getPageIndex() <= 0L){
            vo.setPageSize(1L);
        }
        if(vo.getPageSize() == null || vo.getPageSize() <= 0L){
            vo.setPageSize(10L);
        }
        IPage<GoodsRespVo> pages = new Page<>(vo.getPageIndex(), vo.getPageSize());
        pages = baseMapper.selectGoodsPage(pages, vo);
        return pages;
    }

    @Override
    public GoodsRespVo findGoods(Long id) {
        GoodPageReqVo vo = new GoodPageReqVo();
        vo.setGoodId(id);
        IPage<GoodsRespVo> pageGoods = this.findPageGoods(vo);
        return pageGoods.getRecords().size() > 0 ?  pageGoods.getRecords().get(0) : new GoodsRespVo();
    }

}
